package com.jeesite.modules.tk.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.em.MyTaskStatus;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import com.jeesite.modules.tk.entity.TkTaskWeiquan;
import com.jeesite.modules.tk.service.TkTaskWeiquanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.tk.entity.TkMyTask;
import com.jeesite.modules.tk.service.TkMyTaskService;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * tk_my_taskController
 * @author 李寒冰
 * @version 2025-02-10
 */
@Controller
@RequestMapping(value = "${adminPath}/tk/tkMyTask")
public class TkMyTaskController extends BaseController {

	@Autowired
	private TkMyTaskService tkMyTaskService;
    @Autowired
    private IcUserService icUserService;

	@Value(value = "${pic.reqUrl}")
	private String reqUrl;
    @Autowired
    private TkTaskWeiquanService tkTaskWeiquanService;

	@Autowired
	RestTemplate restTemplate;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TkMyTask get(String id, boolean isNewRecord) {
		return tkMyTaskService.get(id, isNewRecord);
	}


	/**
	 * 维权中的交单明细
	 * @return
	 */
	@RequestMapping(value = {"tkMyTaskInfo", ""})
	public String tkMyTaskInfo(TkMyTask tkMyTask, Model model){
		TkMyTask tkMyTask1 = tkMyTaskService.get(tkMyTask);
		String userId = tkMyTask.getUserId();
		String taskUserId = tkMyTask.getTaskUserId();
		IcUser icUser = icUserService.get(userId);
		IcUser icUser1 = icUserService.get(taskUserId);
		tkMyTask1.setYonghuUser(icUser);
		tkMyTask1.setIcUserShangjia(icUser1);

		String buzhouList = tkMyTask1.getBuzhouList();
		JSONArray jsonArray = JSONArray.parseArray(buzhouList);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String userPic = jsonObject.getString("userPic");
			if(StringUtils.isNotEmpty(userPic) && !userPic.startsWith("http")){
				jsonObject.put("userPic", userPic);
			}
			String pic = jsonObject.getString("pic");
			if(StringUtils.isNotEmpty(pic) && !pic.startsWith("http")){
				jsonObject.put("pic", pic);
			}
			jsonArray.set(i, jsonObject);
		}
		tkMyTask1.setBuzhouList(jsonArray.toJSONString());
		tkMyTask1.setWeiquanId(tkMyTask.getWeiquanId());
		model.addAttribute("reqUrl", reqUrl);
		model.addAttribute("tkMyTask", tkMyTask1);
		TkTaskWeiquan tkTaskWeiquan = tkTaskWeiquanService.get(tkMyTask.getWeiquanId());
		model.addAttribute("tkTaskWeiquan",tkTaskWeiquan );
		JSONArray liucheng = JSONArray.parseArray(tkTaskWeiquan.getLiucheng());
		for (int i = 0; i < liucheng.size(); i++) {
			JSONObject jsonObject = liucheng.getJSONObject(i);
			String pics = jsonObject.getString("pics");
			JSONArray picArr = JSONArray.parseArray(pics);
			jsonObject.put("pics",picArr);
		}
		model.addAttribute("liucheng",liucheng );
		String pics = tkTaskWeiquan.getPics();
		JSONArray jsonArray1 = JSONArray.parseArray(pics);
		model.addAttribute("picArr",jsonArray1 );


		return "modules/tk/tkMyTaskInfo";
	}

	/**
	 * 用户任务详情
	 * @param tkMyTask
	 * @param model
	 * @return
	 */
	@RequiresPermissions("tk:tkMyTask:view")
	@RequestMapping(value = {"taskInfo", ""})
	public String taskInfo(TkMyTask tkMyTask, Model model) {
		TkMyTask tkMyTask1 = tkMyTaskService.get(tkMyTask);
		String userId = tkMyTask.getUserId();
		IcUser icUser = icUserService.get(userId);
		tkMyTask1.setYonghuUser(icUser);

		String buzhouList = tkMyTask1.getBuzhouList();
		JSONArray jsonArray = JSONArray.parseArray(buzhouList);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String userPic = jsonObject.getString("userPic");
			if(StringUtils.isNotEmpty(userPic) && !userPic.startsWith("http")){
				jsonObject.put("userPic", userPic);
			}
			String pic = jsonObject.getString("pic");
			if(StringUtils.isNotEmpty(pic) && !pic.startsWith("http")){
				jsonObject.put("pic", pic);
			}
			jsonArray.set(i, jsonObject);
		}
		tkMyTask1.setBuzhouList(jsonArray.toJSONString());
		model.addAttribute("reqUrl", reqUrl);
		model.addAttribute("tkMyTask", tkMyTask1);
		return "modules/tk/myTaskInfo";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("tk:tkMyTask:view")
	@RequestMapping(value = {"list", ""})
	public String list(TkMyTask tkMyTask, Model model) {
		model.addAttribute("tkMyTask", tkMyTask);
		return "modules/tk/tkMyTaskList";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("tk:tkMyTask:view")
	@RequestMapping(value = {"listPt", ""})
	public String listPt(TkMyTask tkMyTask, Model model) {
		tkMyTask.setType("3");
		model.addAttribute("tkMyTask", tkMyTask);
		return "modules/tk/tkMyTaskList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("tk:tkMyTask:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TkMyTask> listData(TkMyTask tkMyTask, HttpServletRequest request, HttpServletResponse response) {
		tkMyTask.setPage(new Page<>(request, response));
		Page<TkMyTask> page = tkMyTaskService.findPage(tkMyTask);
		List<TkMyTask> list = page.getList();
		for (TkMyTask tkMyTask1 : list) {
			String userId = tkMyTask1.getUserId();
			IcUser icUser = icUserService.get(userId);
			tkMyTask1.setYonghuUser(icUser);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("tk:tkMyTask:view")
	@RequestMapping(value = "form")
	public String form(TkMyTask tkMyTask, Model model) {
		model.addAttribute("tkMyTask", tkMyTask);
		return "modules/tk/tkMyTaskForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("tk:tkMyTask:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TkMyTask tkMyTask) {
		tkMyTaskService.save(tkMyTask);
		return renderResult(Global.TRUE, text("保存tk_my_task成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("tk:tkMyTask:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TkMyTask tkMyTask) {
		tkMyTaskService.delete(tkMyTask);
		return renderResult(Global.TRUE, text("删除tk_my_task成功！"));
	}


	/**
	 * 审核任务
	 */
	@RequiresPermissions("tk:tkMyTask:edit")
	@RequestMapping(value = "updateMyTask")
	@ResponseBody
	public String updateMyTask(TkMyTask tkMyTask) {
		if(tkMyTask.getMyTaskStatus().equals(MyTaskStatus.WEITONGGUO.getCode()) && StringUtils.isEmpty(tkMyTask.getFailExplain())){
			return renderResult(Global.FALSE, text("请填写未通过原因！"));
		}
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity<TkMyTask> request = new HttpEntity<>(tkMyTask, headers);
		String s = restTemplate.postForObject(reqUrl + "/api/task/updateMyTask", request, String.class);
		JSONObject jsonObject = JSONObject.parseObject(s);
		if(jsonObject.getString("code").equals("200")){
			return renderResult(Global.TRUE, text("操作成功！"));
		}else{
			return renderResult(Global.FALSE, text(jsonObject.getString("message")));
		}
	}
	
}